﻿var dateCurret = new Date();
var YearCurrent = dateCurret.getFullYear();

var _nameSubDomain = "";
var domain_url = "";

var _tabMainPage;

$(document).ready(function () {

    $("#tabsMainPage").tabs();
    $(".page_home_content_dashbord").height($(document).height() - 120);
    setTimeout(function () {

        $(".page_home_content_dashbord").layout();

    }, 500);

    domain_url = location.protocol + '//' + location.hostname + (location.port ? ':' + location.port : '');

});

function ChangePassword() {
    loadwindowOnContent(_nameSubDomain + "/Account/ChangePassword", "ChangePassItesMe", "Thay đổi mật khẩu", 350, 270);
}

function formatterDateDDMMYY(date) {
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    var d = date.getDate();
    return (d < 10 ? ('0' + d) : d) + '/' + (m < 10 ? ('0' + m) : m) + "/" + y;
}

function parserDateDDMMYY(s) {
    if (!s) return new Date();
    var ss = (s.split('/'));
    var y = parseInt(ss[0], 10);
    var m = parseInt(ss[1], 10);
    var d = parseInt(ss[2], 10);
    if (!isNaN(y) && !isNaN(m) && !isNaN(d)) {
        return new Date(d, m - 1, y);
    } else {
        return new Date();
    }
}

function parserDateDDMMYYGird(s) {
    if (!s) return new Date();
    var datfull = s.split('T')
    var timeFull = (datfull[1].split(':'));
    var ss = (datfull[0].split('-'));
    var y = parseInt(ss[0], 10);
    var m = parseInt(ss[1], 10);
    var d = parseInt(ss[2], 10);
    return (d < 10 ? ('0' + d) : d) + '/' + (m < 10 ? ('0' + m) : m) + "/" + y + "  " + timeFull[0] + ":" + timeFull[1]  ;
}

function formatDateDataGird(value, row) {
    var d = new Date(value).getTimezoneOffset();
    return formatterDateDDMMYY(d);
}

function formatDateJson(dat) {

    var dtString = "";

    if (dat.substring(0, 6) == "/Date(") {
        var dt = new Date(parseInt(dat.substring(6, dat.length - 2)));
        dtString = dt.getDate() + "/" + (dt.getMonth() + 1) + "/" + +dt.getFullYear();
    }

    return dtString;
}

function getwidthcolum(percen) {
    return (percen * ($(window).width() - 30)) / 100;
}

function getwidthcolumbyDiv(div,percen) {
    return (percen * ($("#"+div).width() - 30)) / 100;
}

function AddTab(selector) {

    var _titleViews = (selector.innerText || selector.textContent).replace(/(\r\n|\n|\r)/gm, "");
    var _urlViews = selector.attributes["data-url"].nodeValue;

    if ($('#tabsMainPage').tabs('exists', _titleViews)) {
        $('#tabsMainPage').tabs('select', _titleViews);
    } else {
        $('#tabsMainPage').tabs('add', {
            title: _titleViews,
            href : _nameSubDomain + _urlViews,
            closable: true
        });
    }

}

function AddTabByText(title, url) {

    var _titleViews = title.replace(/(\r\n|\n|\r)/gm, "");

    if ($('#tabsMainPage').tabs('exists', _titleViews)) {
        $('#tabsMainPage').tabs('select', _titleViews);
    } else {
        $('#tabsMainPage').tabs('add', {
            title: _titleViews,
            href: url,
            closable: true
        });
    }

}

function closeTab() {
    var tab = $('#tabsMainPage').tabs('getSelected');
    if (tab) {
        var index = $('#tabsMainPage').tabs('getTabIndex', tab);
        $('#tabsMainPage').tabs('close', index);
    }
}

function closeTabByName() {
    var tab = $('#tabsMainPage').tabs('getSelected');
    if (tab) {
        var index = $('#tabsMainPage').tabs('getTabIndex', tab);
        $('#tabsMainPage').tabs('close', index);
    }
}

function makeloading(selectId) {
    $("#" + selectId).mask("Dữ liệu đang xử lý...");
}

function makeloadingClass(selectId) {
    $("." + selectId).mask("Dữ liệu đang xử lý...");
}


function loadwindowOnContent(url, elements, title, width, height) {

    var window_currently = $("<div></div>");
    window_currently.appendTo("body").addClass("dialog_window").attr("id", elements);

    window_currently.window({
        draggable: true,
        height: height,
        modal: true,
        pinned: false,
        href: url,
        resizable: true,
        title: title,
        width: width,
        closable: false
    });
}

function close_window_elements(elements) {
    var data_window = $("#" + elements);
    data_window.window('close');
    data_window.window('destroy');
}

function confirmOptionNormal(texts) {
    $.confirm({
        text: texts,
        confirm: function (button) {
        },
        cancel: function (button) {

        }
    });
}

function formatFileSize(bytes) {
    if (typeof bytes !== 'number') {
        return '';
    }

    if (bytes >= 1000000000) {
        return (bytes / 1000000000).toFixed(2) + ' GB';
    }

    if (bytes >= 1000000) {
        return (bytes / 1000000).toFixed(2) + ' MB';
    }

    return (bytes / 1000).toFixed(2) + ' KB';
}

function remove_unicode(str) {
    str = str.toLowerCase();
    str = str.replace(/à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ/g, "a");
    str = str.replace(/è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ/g, "e");
    str = str.replace(/ì|í|ị|ỉ|ĩ/g, "i");
    str = str.replace(/ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ/g, "o");
    str = str.replace(/ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ/g, "u");
    str = str.replace(/ỳ|ý|ỵ|ỷ|ỹ/g, "y");
    str = str.replace(/đ/g, "d");
    str = str.replace(/!|@|%|\^|\*|\(|\)|\+|\=|\<|\>|\?|\/|,|\.|\:|\;|\'| |\"|\&|\#|\[|\]|~|$|_/g, "-");

    str = str.replace(/-+-/g, "-"); //thay thế 2- thành 1- 
    str = str.replace(/^\-+|\-+$/g, "");

    return str;
}

function SetCheckboxListSingle(cblId, portChooseValue) {
    $('#' + cblId).find('input[type="checkbox"]').each(function () {
        $(this).bind('click', function () {
            
            var clickedCbxId = $(this).attr('id');

            $('input.inputcheckPortDauNoi').not(this).prop('checked', false);

            if (this.checked == true) {
                $("#" + portChooseValue).val(clickedCbxId);
            }
            else {
                $("#" + portChooseValue).val("");
            }

        });
    });
}



function PrintAreaDiv(_select) {

    $("#" + _select).print();
    // Cancel click event.
    return (false);

}

function countProperties(obj) {
    var prop;
    
    propCountJson = 0;

    for (var i= 0; i < obj.length; i++) {

        propCountJson = propCountJson + CountObjectChild(obj[i]);
        propCountJson++;
    }
    return propCountJson;
}

function CountObjectChild(children) {

    var prop;
    var propCountJson = 0;
    
    if (children.children.length > 0) {

        for (var i = 0; i < children.children.length; i++) {
            var ProjectrCount = CountObjectChild(children.children[i]);
            propCountJson = propCountJson + ProjectrCount;
        }
    }
    else {
        propCountJson++;

    }

    return propCountJson;

}